// pages/details/details.js
import {createStoreBindings} from 'mobx-miniprogram-bindings'
import {store} from '../../store/store'
import {request} from '../../util/request'
import {timer} from '../../util/time'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        item:'',
        fasttime:'',
        slowtime:'',
        id:'',
        count:0,
        cheacktime:'',
        flag:false,
        settimer:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    //将用户选择的东西加入数据库
    addthings(){
        this.data.count++;
        this.setData({count:this.data.count++})
        let time1 = Date.now();
        clearTimeout(this.data.settimer);
         this.data.settimer = setTimeout(()=>{
            request({
                url:'http://localhost:8000/api/wechat/all',
                method:'get'
            }).then(res=>{
                request({
                    url:'http://localhost:8000/api/wxorders/add',
                    method:'post',
                    data:{
                        useid:res.data.data.openid,
                        id:this.data.id,
                        state:0,
                        name:this.data.item.title,
                        unit_price:this.data.item.currentPrice,
                        Price:this.data.item.originalPrice,
                        unit:'件',
                        num:this.data.count,
                        img:this.data.item.photos
                    }
                }).then(res=>{
                    console.log(res)
                    // clearTimeout(this.data.settimer);
                })
            })
        },2000) 
    },
    //查询该商品是否在数据库，状态为0
    searchone(){
        request({
            url:'http://localhost:8000/api/wechat/all',
            method:'get'
        }).then(res=>{
            // console.log('ssssss',res)
            request({
                url:"http://localhost:8000/api/wxorders/search",
                method:'post',
                data:{
                    openid:res.data.data.openid,
                    id:this.data.id
                }
            }).then(res=>{
                console.log('ssssssssssss',res)
                if(res.data.result.length!==0){
                    this.setData({count:(res.data.result)[0].num})
                }
            }).catch(err=>{
                console.log(err)
            })
        })
    },
    gohome(){
        wx.switchTab({
            url: '/pages/index/index',
          })
    },
    gocart(){
        wx.switchTab({
          url: '/pages/shopping/shopping',
        })
    },
    onLoad(options) {
        this.searchone()
        this.setData({id:options.id})
        let times = timer(Date.now());
        this.setData({fasttime:(times.fasttime.split(' '))[1]})
        this.setData({slowtime:(times.slowtime.split(' '))[1]})
        this.storeBindings = createStoreBindings(this,{
            store,
            fields:['list'],
            actions:['addlist']
        })
        //从store中取出id对应商品
        console.log(store.list)
        if(store.list.length!=0){
            // console.log(123)
            let res = store.list.find(item=>item._id===options.id)
            this.setData({item:res})
            return
        }
        request({
            url:"http://localhost:8000/api/addgoods/findone",
            method:'post',
            data:{id:options.id},
        }).then(res=>{
            this.setData({item:res.data.data})
        })
    
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        this.storeBindings.destroyStoreBindings();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})